package com.asdc.autoscanfiles.repository;

import com.asdc.autoscanfiles.entity.Dict;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface DictRepository extends JpaRepository<Dict, Integer>,
        JpaSpecificationExecutor<Dict> {
	public final static String TABLE_NAME = "Dict";
    @Query(value = "SELECT DISTINCT f " +
            "FROM "+TABLE_NAME+" f " +
            "WHERE f.code = :code and f.type=:type")
    List<Dict> queryByCodeAndType(@Param("code") String code, @Param("type") String type);

    @Query(value = "SELECT DISTINCT f " +
            "FROM "+TABLE_NAME+" f " +
            "WHERE f.type=:type and parentId is null")
    List<Dict> queryParentByType(@Param("type") String type);

    @Query(value = "SELECT DISTINCT f " +
            "FROM "+TABLE_NAME+" f " +
            "WHERE f.type=:type and parentId=:parentId")
    List<Dict> queryByType(@Param("type") String type, @Param("parentId") Integer parentId);

    @Query(value = "SELECT DISTINCT f " +
            "FROM "+TABLE_NAME+" f " +
            "WHERE f.type=:type and parentId is not null")
    List<Dict> queryByType(@Param("type") String type);

}
